package edu.gyc.mytest;

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class HiPanel extends JPanel implements Runnable {

    private static final int NUMS=10000;


    private Point point = new Point(100, 200);
    private Point[] points = new Point[NUMS];

    public HiPanel() {
        //super();
       // this.setBackground(new Color(0x444444));

        makePoints();
    }
//x_max为panel对象的宽度-50 ，y_max为高度-50
    private int x_max=800-50;
    private int y_max=600-50;

    private void getBound() {
        x_max=getWidth()-50;
        y_max=getHeight()-50;
    }

    public void makePoints() {
        getBound();
        Random random = new Random();
        for (int i = 0; i < NUMS; i++) {
            //x,y 符合正态分布，x_max/2为屏幕的中心点
            int x = (int) (random.nextGaussian()*100+x_max/2);
            int y=(int)(random.nextGaussian()*100+y_max/2);
            if (x > x_max) {

                x=x_max;
            }
            if (y > y_max) {
                y=y_max;
            }
            points[i] = new Point(x, y);

        }
    }


    private Color[] colors={Color.red,Color.blue,Color.black,Color.orange,Color.pink};
    int count=0;
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(colors[count++%5]);
        for (Point point : points) {

            g.fillRect(point.getX(),point.getY(),3,3);
        }


      //  g.fillOval(point.getX(), point.getY(), 9, 9);
    }

    private static final Random randomGen = new Random();


    private Timer timer = new Timer();

    class MyTask extends TimerTask {

        @Override
        public void run() {

             makePoints();
            System.out.println( HiPanel.this.getSize());
            HiPanel.this.repaint();
        }
    }
    @Override
    public void run() {
        timer.schedule(new MyTask(),0,1000);
    }


}
